/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import Controller.AddressBook;
import Model.Contact;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author romaindutartre
 */
public class addressBookInterface extends JFrame implements ActionListener,ListSelectionListener {

    private static final String APP_NAME_ = "Address Book";
    private static final String FIRST_NAME_ = "First Name";
    private static final String LAST_NAME_ = "Last Name";
    private static final String ADDRESS_ = "Address";
    private static final String PHONE_ = "Phone";
    private static final String WEBSITE_ = "Website";
    private static final String EMAIL_ = "Email";
    private static final String GROUP_ = "Group";

    private JLabel firstNameLabel, lastNameLabel, addressLabel, phoneLabel,
            websiteLabel, emailLabel, groupLabel;

    private JTextField firstNameText, lastNameText, addressText, phoneText,
            websiteText, emailText, groupText;

    private JTable nameListTable;

    private JTextField firstNameText2, lastNameText2, addressText2, phoneText2,
            websiteText2, emailText2, groupText2;



    private JScrollPane nameList;
    private JPanel infoPanel;
    private JPanel container;
    private JFrame cForm;


    private JButton addBtn, okBtn;

    private AddressBook controller;
    public DefaultTableModel defaultTableModel;

    public ArrayList<Contact> contactsList;

    public void updateInfoPanel(Contact c) {

        firstNameText.setText(c.getFirstName());
        lastNameText.setText(c.getLastName());
        addressText.setText(c.getAddress());
        phoneText.setText(c.getPhone());
        websiteText.setText(c.getWebsite());
        emailText.setText(c.getEmail());
        groupText.setText(c.getGroup());
    }

    public void loadFromCSV() {
        Thread t = new Thread(new Runnable(){


            @Override
            public void run() {
                Scanner inputStream;
                File fichier = new File("src/contactList.csv");

                int id = 0;

                try {
                    inputStream = new Scanner(fichier);
                    while (inputStream.hasNext()) {
                        String data = inputStream.nextLine();
                        String dataTab[] = data.split(";");

                        contactsList.add(new Contact(id,dataTab[0], dataTab[1], dataTab[2], dataTab[3], dataTab[4], dataTab[5], dataTab[6]));
                        defaultTableModel.addRow(new Object[]{id, dataTab[0], dataTab[1], dataTab[6]});
                        id++;
                    }
                    inputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }



        public addressBookInterface(AddressBook that) {

            controller = that;
            contactsList = new ArrayList<Contact>();

            defaultTableModel = new DefaultTableModel();

            defaultTableModel.addColumn("ID");
            defaultTableModel.addColumn(FIRST_NAME_);
            defaultTableModel.addColumn(LAST_NAME_);
            defaultTableModel.addColumn(GROUP_);

            nameListTable = new JTable(defaultTableModel);
            loadFromCSV();

            nameList = new JScrollPane(nameListTable);

            container = new JPanel();
            container.setLayout(new GridLayout(1,2));

            infoPanel = new JPanel();
            infoPanel.setLayout(new GridLayout(8, 2, 0, 25));

            firstNameLabel = new JLabel(FIRST_NAME_);
            lastNameLabel = new JLabel(LAST_NAME_);
            addressLabel = new JLabel(ADDRESS_);
            phoneLabel = new JLabel(PHONE_);
            websiteLabel = new JLabel(WEBSITE_);
            emailLabel = new JLabel(EMAIL_);
            groupLabel = new JLabel(GROUP_);

            firstNameText = new JTextField();
            lastNameText = new JTextField();
            addressText = new JTextField();
            phoneText = new JTextField();
            websiteText = new JTextField();
            emailText = new JTextField();
            groupText = new JTextField();

            addBtn = new JButton("ADD");

            infoPanel.add(firstNameLabel);
            infoPanel.add(firstNameText);
            infoPanel.add(lastNameLabel);
            infoPanel.add(lastNameText);
            infoPanel.add(addressLabel);
            infoPanel.add(addressText);
            infoPanel.add(phoneLabel);
            infoPanel.add(phoneText);
            infoPanel.add(websiteLabel);
            infoPanel.add(websiteText);
            infoPanel.add(emailLabel);
            infoPanel.add(emailText);
            infoPanel.add(groupLabel);
            infoPanel.add(groupText);
            infoPanel.add(addBtn);

            addBtn.addActionListener(this);

            container.add(nameList);

            nameListTable.getSelectionModel().addListSelectionListener(this);

            updateInfoPanel(contactsList.get(0));
            container.add(infoPanel);

            this.add(container);

            setResizable(false);
            setTitle(APP_NAME_);
            setSize(800, 600);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }

    private void contactForm() {
        cForm = new JFrame();
        cForm.setLayout(new GridLayout(8, 2, 0, 25));

        okBtn = new JButton("OK");
        okBtn.addActionListener(this);



        firstNameText2 = new JTextField();
        lastNameText2 = new JTextField();
        addressText2 = new JTextField();
        phoneText2 = new JTextField();
        websiteText2 = new JTextField();
        emailText2 = new JTextField();
        groupText2 = new JTextField();

        cForm.add(firstNameLabel);
        cForm.add(firstNameText2);
        cForm.add(lastNameLabel);
        cForm.add(lastNameText2);
        cForm.add(addressLabel);
        cForm.add(addressText2);
        cForm.add(phoneLabel);
        cForm.add(phoneText2);
        cForm.add(websiteLabel);
        cForm.add(websiteText2);
        cForm.add(emailLabel);
        cForm.add(emailText2);
        cForm.add(groupLabel);
        cForm.add(groupText2);
        cForm.add(okBtn);

        cForm.add(infoPanel);
        cForm.setResizable(false);
        cForm.setSize(400,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        cForm.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
                /*Handle pressed buttons*/
        if (e.getSource()==addBtn) {
            contactForm();
        }
        if(e.getSource() == okBtn) {
            int newID = contactsList.size();
            Contact c = new Contact(newID, firstNameText2.getText(),lastNameText2.getText(), addressText2.getText(), phoneText2.getText(),
                    websiteText2.getText(), emailText2.getText(), groupText2.getText());

            defaultTableModel.addRow(new Object[]{ newID, firstNameText2.getText(),lastNameText2.getText(), groupText2.getText()});

            cForm.setVisible(false);

        }
    }

    public void valueChanged(ListSelectionEvent e) {

        updateInfoPanel(contactsList.get(nameListTable.getSelectedRow()));

    }
}
